package gui_classes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import domain.*;
import java_persistence.*;

/*
 * represents panel for Nurse's Home page
 */
public class NursePanel extends JPanel {
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JButton btnViewPatient;
	private JButton btnAddPatient;
	
//	private User nurse;
	
	/**
	 * Create the panel.
	 */
	public NursePanel(User usr) {
		setLayout(null);
		setPreferredSize(new Dimension(600, 300));
	//	this.nurse = nur;
		GUI_Main.getFrame().setTitle("Logged in as " + usr.getName());
		
		ArrayList<String> appointments = new ArrayList<String>();

		List<Patient> patients = GUI_Main.getSession().getMapper(PatientMapper.class).get_patients(); // no such method.. why?
		int i=0;
		String[] patStr = new String[patients.size()];
		for( Iterator<Patient> patientsIter = patients.iterator(); patientsIter.hasNext(); ){
			Patient cur = patientsIter.next();
			List<String> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(cur.getUsername());
			for(Iterator<String>  appsIter = apps.iterator(); appsIter.hasNext();){
				String current_app = cur.getUsername() + " " + appsIter.next();
				appointments.add(current_app);
			}
			patStr[i]=cur.getName() + " " + cur.getUsername();
			i++;
		}
		
		int j=0;
		String[] appStr = new String[appointments.size()];
		for(Iterator<String> appsIter = appointments.iterator(); appsIter.hasNext();){
			appStr[j] = appsIter.next();
			j++;
		}

		comboBox = new JComboBox(patStr);
		comboBox.setBounds(35, 65, 218, 24);
		add(comboBox);
		
		JLabel lblViewPatients = new JLabel("View Patients");
		lblViewPatients.setBounds(90, 38, 112, 15);
		add(lblViewPatients);
		
		btnAddPatient = new JButton("Add Patient");
		btnAddPatient.setBounds(70, 153, 132, 25);
		btnAddPatient.addActionListener(new AddPatientListener());
		add(btnAddPatient);
		
		btnViewPatient = new JButton("View Patient");
		btnViewPatient.setBounds(70, 116, 132, 25);
		btnViewPatient.addActionListener(new ViewPatientListener());
		add(btnViewPatient);
		
		// Get a list of all appointments
//		List<Appointment> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(pu);
//		List<Appointment> apps = new List<Appointment>();
//		ArrayList<Appointment> apps = new ArrayList<Appointment>();
//		List<String> appointments = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(pu)

		JComboBox comboBox_1 = new JComboBox(appStr);
		comboBox_1.setBounds(317, 65, 271, 24);
		add(comboBox_1);
		
		JLabel lblViewUpcomingAppointments = new JLabel("View Upcoming Appointments");
		lblViewUpcomingAppointments.setBounds(341, 38, 218, 15);
		add(lblViewUpcomingAppointments);
		
	/*	JButton btnView = new JButton("View");
		btnView.setBounds(392, 153, 117, 25);
		btnView.addActionListener(new viewListener());
		add(btnView); */
		

	}
	
	/*
	 * returns the combobox of patients
	 * @return comboBox
	 */
	public JComboBox getComboBox()
	{
		return this.comboBox;
	}
	
	public JComboBox getComboBox1()
	{
		return this.comboBox_1;
	}
	
	/*
	 * getter for View Patient Button
	 */
	public JButton getVButton()
	{
		return this.btnViewPatient;
	}
	
	/*
	 * getter for Add Patient button
	 */
	public JButton getAButton()
	{
		return this.btnAddPatient;
	}
	
	/*
	 * Listener for View patient button
	 */
	private class ViewPatientListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			String[] patnames = ( (String) GUI_Main.npage.getNPanel().getComboBox().getSelectedItem() ).split(" ");
			System.out.println("Nurse wants to view patient " + patnames[patnames.length-1]);
			Patient pat = GUI_Main.getSession().getMapper(PatientMapper.class).get_patient_by_username(patnames[patnames.length-1]);
			
			GUI_Main.viewpinfo = new ViewPatientPage(pat, GUI_Main.npage);
			GUI_Main.viewpinfo.setPrevious(GUI_Main.npage);
			GUI_Main.viewpinfo.display();
			
		}
	}
	
	/*
	 * Listener for Add patient listener
	 */
	private class AddPatientListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			System.out.println("Adding a patient");
			
		/*	GUI_Main.displayAddPatientPage();
			GUI_Main.addpatientPage.setPrevious(GUI_Main.npage); */
			
			GUI_Main.addpatientPage = new AddPatientPanel();
			GUI_Main.addpatientPage.setPrevious(GUI_Main.npage);
			GUI_Main.addpatientPage.display();
			
		}
	}
	
	private class viewListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			String nameplusdate = (String) GUI_Main.npage.getNPanel().getComboBox1().getSelectedItem();   // this is the date of the appointment
			String[] nameanddate = nameplusdate.split(" ");
			String patient_username = nameanddate[0];
			String date = nameanddate[1];
			
			String complaint = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_complaint(date, patient_username);
			
			String[] dates = date.split(" ");
			String day = dates[0];
			String time = dates[1];
			GUI_Main.tscreen = new TreatmentScreen(GUI_Main.getSession().getMapper(PatientMapper.class).get_patient_by_username(patient_username), GUI_Main.npage);
			GUI_Main.tscreen.setPrevious(GUI_Main.npage);
			GUI_Main.tscreen.setTextArea(complaint);
			GUI_Main.tscreen.setDate(day);
			GUI_Main.tscreen.setTime(time);
			GUI_Main.tscreen.display();
		}
	}
	
	
}
